
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Container;

namespace Notification
{
	public class NotificationBizRules : Syrinx.BaseBizRules
	{
		public IBizObjList getNotificationEmailArticles(IApplication app, IRuleContext context)
		{
			context.put("listDetailLevel", DetailLevel.idAndTitle);
			context.put("includeTranslations", true);
			context.put("includeSelf", true);
			context.put("parentId", "SystemEmails");
			IResults rc = context.RuleGroup.execute("getArticles", context, (IBizObj)null);
			IBizObjList articles = rc.CallResults as IBizObjList;
			if (articles == null && rc.ExceptionInfo != null)
				throw rc.ExceptionInfo;
			if (articles != null && articles.numElements == 0)
			{
				IBizObj obj = articles.insertNew();
				Swaf.BGuid.Guid pid = null;
				string uniqueTitle = "SystemEmails";
				obj.put("isSystem", true);
				obj.put("id", new Swaf.BGuid.Guid());
				obj.put("baseTitle", "System Event Notification Emails");
				obj.put("uniqueTitle", uniqueTitle);
				obj.put("baseSummary", "Child articles are used as emails that can be sent when system events occur.");
				obj.put("treeName", "Articles");
				rc = app.ruleMgr.execute("updateArticle", obj);

			}
			return articles;
		}

		public IBizObjList getNotificationEvents(IApplication app, IRuleContext context)
		{
			IBizObjList events = app.bizObjMgr.createList("NotificationEvent");
			FlexiMap msgNames = app.appData.get("notificationEvents") as FlexiMap;
			foreach (string name in msgNames.Keys)
				events.insertNew().put("name", name);
			return events;
		}

		public IBizObjList getNotificationObservers(IApplication app, IRuleContext context)
		{
			context.put("companyId", ActiveCompanyId);
			IBizObjList notificationObservers = app.appData.get("DA.Notification.GetAllNotificationObservers", context) as IBizObjList;

			return notificationObservers;
		}

		public IBizObjList getNotificationObserversForEvent(IApplication app, IRuleContext context, string notificationEventName)
		{
			context.put("companyId", ActiveCompanyId);
			context.put("eventId", notificationEventName);
			IBizObjList notificationObservers = app.appData.get("DA.Notification.GetNotificationObservers", context) as IBizObjList;

			return notificationObservers;
		}

		public void updateNotifyObserver(IApplication app, IRuleContext context, IBizObj notify)
		{
			notify.put("companyId", ActiveCompanyId);
			if (notify.isNew)
				app.appData.put("DA.Notification.AddNotificationObserver", notify);
			else
				app.appData.put("DA.Notification.UpdateNotificationObserver", notify);
		}

		public void deleteNotifyObserver(IApplication app, IRuleContext context, Swaf.BGuid.Guid notifyId)
		{
			context.put("id", notifyId);
			app.appData.put("DA.Notification.DeleteNotificationObserver", context);
		}

		public void notifyObservers(IApplication app, IRuleContext context, string notificationEventName, INameAddressSupport info)
		{
			IResults rc = context.RuleGroup.execute("getNotificationObserversForEvent", notificationEventName);
			IBizObjList observers = rc.CallResults as IBizObjList;
			if (observers != null)
			{
				IDictionary dinfo = info as IDictionary;
				if(dinfo == null)
					dinfo = new NameAddressSupportDictionaryWrapper(info);
				FlexiMap m = new FlexiMap();
				foreach (IBizObj observer in observers)
				{
					rc = context.RuleGroup.execute("getArticleDetail", observer.get("articleId"), true, DetailLevel.fullDetails, false, true);
					IBizObj article = rc.CallResults as IBizObj;
					if (article != null)
					{
						m["emailTo"] = app.resMgr.resolve(observer.get("emailTo").ToString(), dinfo);
						m["emailFrom"] = app.resMgr.resolve(observer.get("emailFrom").ToString(), dinfo);
						m["subject"] = app.resMgr.resolve(article.get("title").ToString(), dinfo);
						m["body"] = app.resMgr.resolve(article.get("body").ToString(), dinfo);

						try
						{
							App.appData.put("Messages.Notification.Basic", m);
						}
						catch (Exception e)
						{
							//TODO: Log fact that this particular email had an exception from sending.
						}
					}
					else
					{
						//TODO: Log fact that an article id is invalid. Bad state in DB?
					}
				}
			}
			else
			{
				//TODO: Exception, could not get observers for event
			}
		}
	}
}
